#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include "Log.hpp"
using namespace LogModule;

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        std::cerr << "Usage: " << argv[0] << " ip port" << std::endl;//使用格式不正确
    }
    Enable_Console_Log_Strategy();
    //初始化客户端
    int fd = socket(AF_INET, SOCK_DGRAM, 0);//成功返回0,失败返回-1
    if(fd < 0)
    {
        LOG(LogLevel::DEBUG) << "socket error";
    }
    LOG(LogLevel::DEBUG) << "socket success: " << fd;
    struct sockaddr_in server;
    bzero(&server, sizeof server);
    server.sin_family = AF_INET;
    server.sin_port = htons(std::stoi(argv[2]));
    server.sin_addr.s_addr = inet_addr(argv[1]);
    

    while(true)
    {
        std::string buffer;
        std::cout << "Please Enter# ";
        std::getline(std::cin, buffer);
        int n = sendto(fd, buffer.c_str(), buffer.size(), 0, (struct sockaddr *)&server, sizeof server);
        (void) n;

        
        char b[1024];
        struct sockaddr_in client;
        uint32_t len = sizeof client;
        int s = recvfrom(fd, b, sizeof b - 1, 0, (struct sockaddr *)&server, &len);
        if(s > 0)
        {
            b[s] = 0;
            std::cout << b << std::endl;
        }
    }
    return 0;
}